<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 青 <99701759@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Admin\Api\NewmemberApi;
use Admin\Api\PostApi;
use Shop\Api\AddressApi;
use Shop\Api\WithdrawApi;
use Think\Controller;
use Ucenter\Api\UserLogApi;
use Weixin\Api\WxuserApi;

class UserCenterController extends HomeController {

    protected function _initialize() {
        parent::_initialize();

    }

    /*
     * 个人中心首页
     * */
    public function index(){
        if($this->newmember['vip_type'] == 1){
            if($this->newmember['role_grade'] == 1){
                $this->assign('flag','1');//表示星级会员可以被邀请   其他会员将不会被邀请  不会提供相应的接口
            }
        }
        $this->display();
    }

    /*
     * 我的资料
     * */
    public function my_info(){
        $this->display();
    }

    /*
     * 我的推荐的   也就是我的下级
     * */
    public function my_junior(){
        $map['invitation_uid'] = $this->userinfo['id'];
        $this->assign('avatar',$this->userinfo['avatar']);
        $this->assign('nickname',$this->userinfo['nickname']);

        $list = apiCall(NewmemberApi::QUERY_NO_PAGING,array($map));
        if($list['status']){
            $count = count($list['info']);
            $this->assign('count',$count);
            if($count > 0){
                $wxid_arr = array_column($list['info'],'wx_id');
                $str = implode(',',$wxid_arr);
                $wxmap['id'] = array('in',$str);
                $list = apiCall(WxuserApi::QUERY_NO_PAGING,array($wxmap));
                if($list['status']){
                    $this->assign('list',$list['info']);
                }
            }
        }
        $this->display();
    }

    /*
     * 申请提现
     * */
    public function apply_money(){
        $map['uid'] = $this->userinfo['id'];
        $moneymap['id'] = $this->userinfo['id'];
        $money = apiCall(WxuserApi::GET_INFO,array($moneymap));
        if($money['status']){
            $this->assign('money',$money['info']['money']);
        }
        $info = apiCall(WithdrawApi::QUERY_NO_PAGING,array($map,'create_time desc'));
        if($info['status']){
            $this->assign('list',$info['info']);
        }
        $this->display();
    }

    /*
     *推广二维码
     * */
    public function spread_code(){
        $token = "pvifkmrw1476152475";
//		dump($token);
        $result = apiCall("Weixin/Wxaccount/getInfo", array(array('token'=>$token)));
        if(!$result['status']){
            LogRecord($result['info'], __FILE__.__LINE__);
            return "无法获取！";
        }
        if(is_null($result['info'])){
            return "Token 无效！";
        }

        $signPackage = $this->wxapi -> getSignPackage();

    	$jsapilist = "'onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo'";
        $config = "wx.config({
			    debug: false, 
			    appId: '" . $signPackage["appId"] . "', 
			    timestamp: '" . $signPackage["timestamp"] . "', 
			    nonceStr: '" . $signPackage["nonceStr"] . "', 
			    signature: '" . $signPackage["signature"] . "',
			    jsApiList: [" . $jsapilist . "]
			});";
        $this->assign('config',$config);
        //下面是二维码的获得

        $asx = $this -> wxapi ->getTcode($this->userinfo['invite_code']);
        $code_imgurl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$asx['msg']->ticket;
        $this->assign('code_imgurl',$code_imgurl);
        $this->assign($this->userinfo);
        $this->display();
    }
    /*
     * 地址列表
     * */
    public function addre_manage(){
        $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
        $user = apiCall(WxuserApi::GET_INFO , array($map));
        $this->assign('d_addr',$user['info']['default_address']);
        $map = array('uid'=>$user['info']['id']);
        $result = apiCall(AddressApi::QUERY_NO_PAGING , array($map));
        $this->assign("addr",$result['info']);
        $this->display();
    }

    /*
     * 积分明细
     * */
    public function integral_detail(){
        $map['uid'] = $this->userinfo['id'];
        $map['num_type'] = 0;//表示积分
        $list = apiCall(UserLogApi::QUERY_NO_PAGING,array($map));

        if($list['status']){
            $this->assign('list',$list['info']);
        }
        $this->display();
    }

    /*
     * 储值
     * */
    public  function stored_value(){
        $postmap['id'] = 3;
        $simpleres = apiCall(PostApi::GET_INFO,array($postmap));
        if($simpleres['status']){
            $this->assign('simple',htmlspecialchars_decode($simpleres['info']['post_content']));
        }

        $map['uid'] = $this->userinfo['id'];
        $map['num_type'] = '1';//表示储值
        $map['op_type'] = 99;
        $list = apiCall(UserLogApi::QUERY_NO_PAGING,array($map));

        if($list['status']){
            $this->assign('list',$list['info']);
        }

        $this->display();
    }

    /*
     * 邀请成为会员 通过邀请码的形式 没有邀请码生成邀请码
     * */
    public function invite_vip(){

        $map['wx_id'] = $this->userinfo['id'];
        $res = apiCall(NewmemberApi::GET_INFO,array($map));
        if($res['status']){
            $this->assign('power',$res['info']['invite_power']);
            if($res['info']['invite_power'] == 1){
                $gold_code = $res['info']['gold_code'];
                $platina_code = $res['info']['platina_code'];
                $str = date('YmdHis');
                $flag = false;
                $this->assign('gold_number',$res['info']['s_gold_mumber']);
                $this->assign('platina_number',$res['info']['s_platina_mumber']);
                if($gold_code == null){
                    $gold_code = $str.createRandomStr(5).$map['wx_id'].'g';
                    $save['gold_code'] = $gold_code;
                    $flag = true;
                }

                if($platina_code == null){
                    $platina_code = $str.createRandomStr(5).$map['wx_id'].'p';
                    $save['platina_code'] = $platina_code;
                    $flag = true;
                }
                if($flag){
                    $res = apiCall(NewmemberApi::SAVE,array($map,$save));
                    if($res['status']){
                        $this->assign('gold_code',$gold_code);
                        $this->assign('platina_code',$platina_code);
                    }
                }else{
                    $this->assign('gold_code',$gold_code);
                    $this->assign('platina_code',$platina_code);
                }
            }
        }
        $this->display();
    }

    /*
     * 会员权益
     * */
    public function vip_equity(){
        $vip_type = $this->newmember['vip_type'];
        $role_grade = $this->newmember['role_grade'];

        if($vip_type == 1){
            if($role_grade == 1){
                $map['id'] = 1;
            }
            if($role_grade == 2){
                $map['id'] = 4;
            }
            if($role_grade == 3){
                $map['id'] = 5;
            }
            if($role_grade == 4){
                $map['id'] = 6;
            }
            if($role_grade < 0){
                $map['id'] = 7;
            }
        }

        if($vip_type == 2){
            $map['id'] = 8;
        }

        if($vip_type == 3){
            $map['id'] = 9;

        }

        $contentres = apiCall(PostApi::GET_INFO,array($map));
        if($contentres['status']){
            $this->assign('content',htmlspecialchars_decode($contentres['info']['post_content']));
        }
        $this->display();
    }



    /*
     * 填写邀请码
     * */
    public function fill_in(){
        if(IS_POST){
            if(!empty(I('code'))){
                $code = I('code');
                $flag = substr($code,strlen($code)-1,1);

                if($flag == 'g'){
                    $map['gold_code'] = $code;
                    $newmember = apiCall(NewmemberApi::GET_INFO,array($map));

                    if($newmember['status']){
                        if($newmember['info']['s_gold_mumber'] > 0){
                            $rsave = array(
                                'buy_num'=>'-1',
                                'role_grade'=>'5',
                            );
                            $psave = array(
                                's_gold_mumber'=>$newmember['info']['s_gold_mumber']-1,
                            );
                            $pmap['id'] = $newmember['info']['wx_id'];
                        }
                    }
                }


                if($flag == 'p'){
                    $map['platina_code'] = $code;
                    $newmember = apiCall(NewmemberApi::GET_INFO,array($map));
                    if($newmember['status']){
                        if($newmember['info']['s_gold_mumber'] > 0){
                            $rsave = array(
                                'buy_num'=>'-1',
                                'role_grade'=>'6',
                            );
                            $psave = array(
                                's_gold_mumber'=>$newmember['info']['s_gold_mumber']-1,
                            );
                            $pmap['id'] = $newmember['info']['wx_id'];
                        }
                    }
                }

                $modal = M();
                $modal->startTrans();
                $flag = false;
                $rmap['id'] = $this->newmember['wx_id'];

                $rres = apiCall(NewmemberApi::SAVE,array($rmap,$rsave));

                if(!$rres['status']){
                    $flag = true;
                }

                $pres = apiCall(NewmemberApi::SAVE,array($pmap,$psave));

                if(!$pres['status']){
                    $flag = true;
                }

                if($flag == true){
                    $modal->rollback();
                    $this->error('该邀请码已经失效');
                }else{
                    $modal->commit();
                    $this->success('受邀成功');
                }
            }

        }else{

            $this->display();

        }

    }
}